package com.burst.reptile.framework.web.exception;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.burst.reptile.common.constant.HttpStatus;
import com.burst.reptile.common.exception.BaseException;
import com.burst.reptile.common.exception.CustomException;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.framework.web.result.Result;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;

/**
 * 全局异常处理器
 * @author wujin
 * @date: 2020年5月21日 下午5:29:44
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	/**
	 * 基础异常
	 */
	@ExceptionHandler(BaseException.class)
	public Result<Object> baseException(BaseException e) {
		log.error("\n【基础异常，异常信息】: {}", e.getMessage());
		return Result.error(e.getMessage());
	}

	/**
	 * 业务异常
	 */
	@ExceptionHandler(CustomException.class)
	public Result<Object> businessException(CustomException e) {
		log.error("\n【业务异常，异常信息】: {}", e.getMessage());
		if (ToolUtil.isEmpty(e.getCode())) {
			return Result.error(e.getMessage());
		}
		return Result.error(e.getCode(), e.getMessage());
	}

	@ExceptionHandler(NoHandlerFoundException.class)
	public Result<Object> handlerNoFoundException(Exception e) {
		log.error("\n【NoHandlerFoundException，异常信息】: {}", e.getMessage());
		return Result.error(HttpStatus.NOT_FOUND, "路径不存在，请检查路径是否正确");
	}

	@ExceptionHandler(AccessDeniedException.class)
	public Result<Object> handleAuthorizationException(AccessDeniedException e) {
		log.error("\n【权限异常，异常信息】: {}", e.getMessage());
		return Result.error(HttpStatus.FORBIDDEN, "没有权限，请联系管理员授权");
	}

	@ExceptionHandler(AccountExpiredException.class)
	public Result<Object> handleAccountExpiredException(AccountExpiredException e) {
		log.error("\n【AccountExpiredException，异常信息】: {}", e.getMessage());
		return Result.error(e.getMessage());
	}

	@ExceptionHandler(UsernameNotFoundException.class)
	public Result<Object> handleUsernameNotFoundException(UsernameNotFoundException e) {
		log.error("\n【UsernameNotFoundException，异常信息】: {}", e.getMessage());
		return Result.error(e.getMessage());
	}

	@ExceptionHandler(Exception.class)
	public Result<Object> handleException(Exception e) {
		e.printStackTrace();
		log.error("\n【服务异常，异常信息】: {}", e.getMessage());
		return Result.error(e.getMessage());
	}

	/**
	 * 自定义验证异常
	 */
	@ExceptionHandler(BindException.class)
	public Result<Object> validatedBindException(BindException e) {
		log.error("\n【验证异常，异常信息】: {}", e.getMessage());
		String message = e.getAllErrors().get(0).getDefaultMessage();
		return Result.error(message);
	}

	/**
	 * 自定义验证异常
	 */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	public Result<Object> validExceptionHandler(MethodArgumentNotValidException e) {
		log.error("\n【MethodArgumentNotValidException，异常信息】: {}", e.getMessage());
		String message = e.getBindingResult().getFieldError().getDefaultMessage();
		return Result.error(message);
	}
	
	
	/**
	 * 微信小程序异常
	 */
	@ExceptionHandler(WxErrorException.class)
	public Result<Object> validExceptionHandler(WxErrorException e) {
		log.error("\n【微信小程序，异常信息】: {}", e.getMessage());
		return Result.error(e.getMessage());
	}

}
